# -*- coding: utf-8 -*-
'''
@文件    :redis.py
@说明    :redis的同步封装
@时间    :2025/08/25 17:25:24
@作者    :浪晋
@版本    :1.0
'''

import redis


class Redis:
    def __init__(self, username: str = "defualt", password: str = "", host: str = "loaclhost", port: int = 6379, db: int = 0, decode: bool = True):
        self.user = username
        self.pwd = password
        self.host = host
        self.port = port
        self.db = db
        self.decode = decode

    def get(self, key):
        return self.pool.get(key)
    
    def set(self, key, value):
        return self.pool.set(key, value)
    
    def startup(self):
        """关闭连接池"""
        url = f"redis://{self.user}:{self.pwd}@{self.host}:{self.port}/{self.db}"
        self.pool = redis.from_url(
            url=url,
            encoding="utf-8",
            decode_responses=self.decode
        )

    def shutdown(self):
        """关闭连接池"""
        self.pool.close()


if __name__ == "__main__":

    def demo():
        redis = Redis("")
        res = redis.get("name")
        print(res)
    
    demo()